import sys

from PySide6.QtCore import QTimer
from PySide6.QtWidgets import QMainWindow, QPushButton, QDialog, QLabel, QProgressBar, QVBoxLayout, QApplication, QHBoxLayout, QWidget
from PySide6 import QtCore

class MyApp(QMainWindow):
    def __init__(self):
        super().__init__(None)
        self.setWindowTitle("时间进度条")
        self.start = QPushButton("显示进度对话框")
        self.start.setContentsMargins(10, 10, 10, 10)
        self.start.clicked.connect(self.start_progress_dialog)

        layout = QHBoxLayout()
        layout.addWidget(self.start)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def start_progress_dialog(self):
        self.progress = ProgressDialog(self, "Title", "Message...")
        self.progress.show()

class ProgressDialog(QDialog):
    def __init__(self, parent, title, message):
        super().__init__(parent)
        self.msg = QLabel(message, self)
        self.progressBar = QProgressBar(self, minimum=0, maximum=1000)
        self.progressBar.setStyleSheet("""
            QProgressBar {
                text-align: center; /* 文本居中 */
                border: 2px solid grey;
                border-radius: 5px;
                padding: 1px;
            }
            QProgressBar::chunk {
                background-color: #05B8CC;
                width: 20px; /* 控制进度块的大小 */
                margin: 0.5px;
            }
        """)
        # 设置进度条的样式表，使文本水平排列
        layout = QVBoxLayout()
        layout.setContentsMargins(30, 20, 30, 20)

        layout.addWidget(self.msg, alignment=QtCore.Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(self.progressBar)

        self.setLayout(layout)
        self.setWindowTitle(title)
        self.setWindowModality(QtCore.Qt.WindowModality.WindowModal)
        self.setWindowFlag(QtCore.Qt.WindowType.WindowCloseButtonHint, False)

        self.timer = QTimer()
        self.timer.setTimerType(QtCore.Qt.TimerType.PreciseTimer)
        self.timer.setInterval(5)
        self.timer.timeout.connect(self.progress_update)
        self.timer.start()

    def progress_update(self):
        cur_value = self.progressBar.value()
        if cur_value < self.progressBar.minimum():
            self.progressBar.setValue(self.progressBar.minimum())
        elif cur_value >= self.progressBar.maximum():
            self.timer.stop()
            self.accept()
        else:
            self.progressBar.setValue(cur_value + 1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    ex.show()
    sys.exit(app.exec())